package edu.hebeu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import edu.hebeu.entity.Department;
import edu.hebeu.entity.Position;
import edu.hebeu.mapper.DepartmentMapper;
import edu.hebeu.mapper.EmployeeMapper;
import edu.hebeu.entity.Employee;
import edu.hebeu.mapper.PositionMapper;
import edu.hebeu.service.EmployeeService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2024-12-05 14:59:56
 */
@Service("employeeService")
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private  EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Override
    public Employee selectcheck(Integer employeeNumber, String password) {
    Employee employee= employeeMapper.selectcheckLog(employeeNumber,password);
        return employee;
    }

    @Override
    public Employee select(Integer id) {
        Employee employee = employeeMapper.selectId(id);
       setObject(employee);
        return employee;
    }

    @Override
    public List<Employee> selectLis() {
        EntityWrapper<Employee> employeeEntityWrapper = new EntityWrapper<>();
        employeeEntityWrapper.orderBy("id",false);
        //List<Employee> employeeList = baseMapper.selectList(employeeEntityWrapper);
        List<Employee> employeeList = employeeMapper.selectList(employeeEntityWrapper);
        return employeeList;
    }

    public List<Employee> selectIncumbency(int pageNo){
//        从数据库分别查询employee和对应的department以及position
//        查询当前分页所有的employee
        List<Employee> employeeList =employeeMapper.selectPage(new RowBounds(pageNo,10), null);
//        根据Employee中的DepartmentNumber,查询当前分页所有的Department
        List<Integer> departmentNumbers = new ArrayList<>();
        for (Employee employee : employeeList) {
            departmentNumbers.add(employee.getDepartmentNumber());
        }
        Wrapper<Department> departmentWrapper = new EntityWrapper<>();
        departmentWrapper.in("department_number",departmentNumbers);
        List<Department> departmentList =departmentMapper.selectList(departmentWrapper);
//        根据Employee中的PositionNumber查询Position
        List<Integer> positionNumbers = new ArrayList<>();
        for (Employee employee : employeeList) {
            positionNumbers.add(employee.getPositionNumber());
        }
        Wrapper<Position> positionWrapper = new EntityWrapper<>();
        positionWrapper.in("position_number",positionNumbers);
        List<Position> positionList =positionMapper.selectList(positionWrapper);
//        将查询的Department和position装配入Employee中
        for (Employee employee : employeeList) {
            for (Department department : departmentList) {
                if (department.getDepartmentNumber() == employee.getDepartmentNumber()) {
                    employee.setDepartment(department);
                }
                for (Position position : positionList) {
                    if (position.getPositionNumber() == employee.getPositionNumber()) {
                        employee.setPosition(position);
                    }
                }
            }
        }
        return employeeList;
    };

    private void setObject(Employee employee) {
        Integer departmentNumber = employee.getDepartmentNumber();
        Department department=departmentMapper.selectByNumber(departmentNumber);
        employee.setDepartment(department);
        Integer positionNumber = employee.getPositionNumber();
          Position position=positionMapper.selectByNumber(positionNumber);
       employee.setPosition(position);
    }


}

